﻿using AutoMapper;
using B.S.ProcessEgine.API.Applocations.Commands.FormDetail;
using B.S.ProcessEgine.API.Dtos.ResponseDto;
using B.S.ProcessEgine.ErrorCode;
using B.S.ProcessEgine.Infractructure.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.ProcessEgine.API.Applocations.CommandHandlers.FormDetail
{
    public class GetFormDetailCommandHandler : IRequestHandler<GetFormDetailCommand, ApiResult<IList<GetFormDetailResponseDto>>>
    {
        private readonly IFormInfoDetailRepository formDetail;
        private readonly IMapper mapper;

        public GetFormDetailCommandHandler(IFormInfoDetailRepository formDetail,IMapper mapper)
        {
            this.formDetail = formDetail;
            this.mapper = mapper;
        }
        public async Task<ApiResult<IList<GetFormDetailResponseDto>>> Handle(GetFormDetailCommand request, CancellationToken cancellationToken)
        {
            var list = await formDetail.GetAll().Where(x => x.FormId == request.FormId&&!x.IsDel).ToListAsync(cancellationToken);

            var dto = mapper.Map<IList<GetFormDetailResponseDto>>(list);

            return ApiResult<IList<GetFormDetailResponseDto>>.Success(dto, StateCodeEnum.Ok);
        }
    }
}
